//
//  VisitorView.swift
//  weibo
//
//  Created by 王、小胜 on 16/7/25.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

class VisitorView: UIView {
    
    //设置访客视图每个界面的视图信息
    func setupInfo(imageName: String?, title: String){
        
        messageLable.text=title
        //如果图片名称为空说明是首页
         guard let imgName = imageName else {
            
            //首页开启动画
            startAnim()
            return
        }
        iconView.image = UIImage(named: imageName!)
        //隐藏小房子
        homeIconView.hidden = true
        //1.隐藏遮罩视图
        //maskIconView.hidden = true
        
        //2.将遮罩视图移动到底端
        sendSubviewToBack(maskIconView)
    }
    
    ///开启首页转轮动画
    private func startAnim() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //用在不断重复的动画上面，当动画绑定的图层对应的视图被销毁，动画会自动销毁
        
        anim.removedOnCompletion = false
        //添加到图层
        iconView.layer.addAnimation(anim, forKey: nil)
    }

    //initwithframe是 uiview 制动的构造函数
    //使用纯代码开发使用
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //调用设置界面
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        //导致如果使用 sb 开发，调用这个视图会直接崩溃
        
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    // MARK - 懒加载控件
    ///转轮
    private lazy var iconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
        ///遮罩图像
    private lazy var maskIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
        ///小房子
    private lazy var homeIconView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
        ///消息文字
    private lazy var messageLable: UILabel = UILabel(title: "关注一些人，鬼这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
    ///注册按钮
     lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), imageName: "common_button_white_disable")
    ///登录按钮
     lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.orangeColor(), imageName: "common_button_white_disable")
}

extension VisitorView{
    ///设置界面
    private func setupUI(){
        //1.添加所有控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLable)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2.设置自动布局
        
        /*
        translatesAutoresizingMaskIntoConstraints  默认是ture 支持使用setframe 的方式设置控件位置
        false 支持使用自动布局来设置控件位置
        */
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //转轮
        //.centerX 水平居中
        //constant 偏移量
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        
        //小房子
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView.snp_center)
        }
//        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        //消息文字
        
        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.height.equalTo(35)
            make.width.equalTo(224)
        }
//        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        //宽度约束
//        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
//        //高度约束
//        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        
        //注册按钮
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLable.snp_left)
            make.top.equalTo(messageLable.snp_bottom).offset(16)
            make.width.equalTo(100)
            make.height.equalTo(35)
        }
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Left, relatedBy: .Equal, toItem: messageLable, attribute: .Left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        ///登录按钮
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLable.snp_right)
            make.top.equalTo(messageLable.snp_bottom).offset(16)
            make.height.equalTo(35)
            make.width.equalTo(100)
        }
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Right, relatedBy: .Equal, toItem: messageLable, attribute: .Right, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom, multiplier: 1.0, constant: 16))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
//        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 36))
        
        ///遮罩视图
        
        /*
        VFL 可视化格式语言
        
        H 水平方向
        V 垂直方向
        | 边界
        [] 包装控件
        
        views 是一个字典[名字：控件名]  -VFL 字符串中表示控件的字符串
        metrics 是一个字典 [名字 : NSNumber] - VFL 字符串中表示某一个数值
        */
        
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIconView]))
//
//        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(btnHeight)-[regButton]", options: [], metrics: ["btnHeight":-36], views: ["mask": maskIconView, "regButton": registerButton]))
        
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(registerButton.snp_bottom)
        }
        
        //设置背景颜色  灰度图
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1.0)
    }
}





